<#include "../../layout/dlgLayout.ftl">
<link href="/static/module/add2.css" rel="stylesheet">
<link href="/static/module/form.css" rel="stylesheet">
<@layout>
<style>
    .layout-form{
        width: 100%;
    }
</style>
<form class="layui-form layout-form" action="/materialsLibrary/update" method="post" id="newItemForm">
    <div class="layui-form-item">
        <label class="layui-form-label" ><span class="input-required"></span>材料名称：</label>
        <div class="layui-input-block">
            <input type="text" name="materialName" class="layui-input" value="${param.materialName!''}">
            <input type="hidden" name="id" class="layui-input" value="${param.id!''}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label" ><span></span>提供部门：</label>
        <div class="layui-input-block">
            <input type="text" name="supplyDept" class="layui-input" value="${param.supplyDept!''}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span></span>是否共享：</label>
        <div class="layui-input-block">
            <select name="share" lay-filter="aaa" id="share">
                <option value="1">共享</option>
                <option value="2">不共享</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item interfaceLink home6" >
        <label class="layui-form-label"><span></span>接口连接：</label>
        <div class="layui-input-block">
            <input type="text" name="interfaceLink" class="layui-input" value="${param.interfaceLink!''}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span></span>点击上传文件：</label>
        <div class="layui-input-block">
		    <button type="button" class="layui-btn" id="uploadBtn"><i class="layui-icon"></i>上传文件</button>
			<ul class="file-list">
			</ul>
	        <input name="uploadFiles" type="hidden">
        </div>
    </div>
    <div class="layui-form-item" >
        <div class="submit-btn-area">
            <button class="layui-btn" lay-submit="">保存</button>
            <button type="button" class="layui-btn layui-btn-primary" onclick="onClose();">取消</button>
        </div>
    </div>
</form>
<script>
    layui.use(['laydate', 'form', 'global', 'jquery.form.min', 'upload'], function () {
        var laydate = layui.laydate, form = layui.form, $ = layui.$, upload = layui.upload;
        
        if('${param.share}'){
        	$('#share').val('${param.share}');
        }
        if('${param.share}'==1) {
        	setTimeout(function(){
        		$('.home6').show();
       		},300)
        }
        $.get('/materialsLibrary/uploadFilesList',{id: ${param.id}}).then(function(result){
        	var fileList = []
        	result.forEach(function(list){
        		var li = '<li><a href="'+list.path+'" target="_blank"><i class="layui-icon layui-icon-file"></i><span>'+ list.fileName+'</span></a><i class="layui-icon layui-icon-close file-remove txt_r" data="'+list.id+'"></i></li>';
                $('.file-list').append(li)
                fileList.push(list.id)
        	})
        	$('input[name="uploadFiles"]').val(fileList.join(','))
        })
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#uploadBtn'
            ,accept: 'file'
            ,url: '${filePath}'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                	
                    $('#renderImg').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                if (res.success) {
                   /*  $('#img-url').html(res.data.path).attr('href', res.data.path).next().val(res.data.path) */
                	 var list = '<li><a href="'+res.data.path+'"><i class="layui-icon layui-icon-file"></i><span>'+ res.data.fileName+'</span></a><i class="layui-icon layui-icon-close file-remove txt_r" data="'+res.data.id+'"></i></li>';
                     $('.file-list').append(list)
                     var fileList = $('input[name="uploadFiles"]').val()
                     if(fileList){
                     	fileList = fileList.split(",")
                     	fileList.push(res.data.id)
                     	fileList = fileList.join(',')
                     }else{
                     	fileList = res.data.id
                     }
                     $('input[name="uploadFiles"]').val(fileList)
                } else {
                    layer.msg('上传失败', {
                        time:2000
                    });
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                layer.msg('上传失败', {
                    time:2000
                });
            }
        });
        $(document).on('click','.file-remove',function(){
        	var fileList = ($('input[name="uploadFiles"]').val()).split(",")
           	fileList.splice(fileList.indexOf($(this).attr('data')),1)
            $('input[name="uploadFiles"]').val(fileList.join(','))
            $(this).parent().remove()
        })
        
        
        $('#newItemForm').ajaxForm({
            success: function (data) {
                if (data.success) {
                	//var obj=JSON.stringify(data);
                    layer.closeAll();
                    layer.msg("新增材料成功。", {
                        time:1000
                    });
                    materialsAddList(data.data, $('[name="materialName"]').val())
                    /* parent.reloadTable(); */
                }
            }
        });
        
        form.on('select(aaa)', function (data) {                   
            if ($('select[name="share"]').val() == "1") {
                $(".interfaceLink").show();
            }
            else if ($('select[name="share"]').val() == "2") {
                $(".interfaceLink").hide();
            }
            form.render();
        });
    
        
        window.onClose = function () {
            layer.closeAll();
        };
        form.render();
    });
    
 
    
    
</script>
</@layout>